# -*- coding: utf-8 -*-
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
import pymysql
# 添加 sql 日志
import logging
from demo.common.logging.logging_config import setup_logging

setup_logging()
# 启用 SQLAlchemy 连接池日志
sqlalchemy_pool_logger = logging.getLogger("sqlalchemy.pool")
sqlalchemy_pool_logger.setLevel(logging.INFO)


pymysql.install_as_MySQLdb()

# SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
SQLALCHEMY_DATABASE_URL = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
    username="root",
    password="PRma-qmLnWf-Q90dA-V91La",
    hostname="127.0.0.1:3306",
    databasename="fast-demo",
)

engine = create_engine(
    SQLALCHEMY_DATABASE_URL, 
    # 加上连接池配置，就能看到请求被卡死
    pool_size=2,
    max_overflow=1,
    pool_recycle=60,
)
# SessionLocal = sessionmaker(autocommit=False, autoflush=True,expire_on_commit=True, bind=engine)
SessionFactory = sessionmaker(autocommit=False, autoflush=True,bind=engine)
ScopedSession = scoped_session(SessionFactory)

Base = declarative_base()

